package com.csist.client;

import com.csist.client.task.ReceiverRunnable;
import com.csist.dao.FriendDao;
import com.csist.dao.MessageDao;
import com.csist.dao.UserDao;
import com.csist.model.Message;
import com.csist.model.User;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Scanner;

public class ClientFunction {
    private static Scanner input = new Scanner(System.in);
    private static UserDao uDao = new UserDao();
    private static FriendDao fDao = new FriendDao();
    private static MessageDao mDao = new MessageDao();

    /**
     * 注册
     */
    public void register(){
        System.out.println("输入用户名：");
        String name = input.next();
        System.out.println("输入密码：");
        String pwd = input.next();
        if (uDao.register(name,pwd)){
            System.out.println("注册成功✔");
        } else {
            System.out.println("注册失败❌");
        }
    }

    /**
     * 登录
     */
    public void login(){
        System.out.println("输入用户名：");
        String name = input.next();
        System.out.println("输入密码：");
        String pwd = input.next();
        User user = uDao.login(name,pwd);
        if(user!=null){
            System.out.println("登录成功");
            connection(user);//登录成功-->连接服务器
        } else {
            System.out.println("登录失败，用户名或密码有误");
        }
    }

    private void connection(User user){
        Socket socket = null;
        try {
            socket = new Socket("127.0.0.1",8888);
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(user);

            //监听消息线程
            new Thread(new ReceiverRunnable(socket)).start();


            while(true){
                System.out.println("1、群聊(输入886退出)   2、私聊>>查看好友000添加好友 0、退出");
                int t = input.nextInt();
                if (t==0) break;
                if (t==1) {
                    groupChat(user,socket);//进入群聊处理
                }
                if (t==2) {
                    privateChat(user,socket);//进入私聊处理
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void groupChat(User sender,Socket socket){
        ObjectOutputStream oos = null;
        while (true){
            System.out.println("输入...");
            String msg = input.next();
            if ("886".equals(msg)) {
                break;
            }
            try {
                oos = new ObjectOutputStream(socket.getOutputStream());
                oos.writeObject(new Message(sender.getId(),msg,0,new Date(System.currentTimeMillis()),false));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private void privateChat(User sender,Socket socket){
        System.out.println("1、查看好友   2、添加好友 ");
        int t = input.nextInt();
        if (t==1) {
            ArrayList<Integer> friends = fDao.getFriendListById(sender.getId());
            System.out.println(friends);
            System.out.println("输入好友id发送信息");
            int target  = input.nextInt();
            boolean canSend = false;
            for (Integer i:friends) {
                if (i == target){
                    canSend = true;
                }
            }
            if (canSend){//输入的好友id正确才能进行发送消息操作
                String msg = input.next();
                //把这条消息存储在数据库中
                mDao.sendMsg(sender.getId(),target,msg);
                try {
                    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                    //这条消息发送给服务器进行处理
                    oos.writeObject(new Message(sender.getId(),msg,target,new Date(System.currentTimeMillis()),false));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if (t==2) {
            int friendId = input.nextInt();
            System.out.println(fDao.addFriend(sender.getId(),friendId)?"添加成功":"添加失败");
        }
    }
}
